<?php

namespace app\common\model\Depot;

use think\Model;
// 引入软删除的模型
use traits\model\SoftDelete;

class Back extends Model
{

    //在模型内部去引入软删除
    use SoftDelete;
    //
    protected $name = "depot_back";

    //指定一个自动设置的时间字段
    //开启自动写入
    protected $autoWriteTimestamp = true;

    //设置字段的名字
    protected $createTime = "createtime"; //插入的时候设置的字段名

    //禁止 写入的时间字段
    protected $updateTime = false;

    //设置软删除的字段更新的时间字段
    protected $deleteTime = 'deletetime';

    protected $append = [
        'createtime_text',
        'thumbs_text',
        'thumb_text',
        'province_text',
        'city_text',
        'district_text',
        'status_text',
        'admin_text',
        'reviewer_text',
        'stroman_text',
    ];

    public function getCreateTimeTextAttr($value,$data){
        $createtime = isset($data['createtime']) ? trim($data['createtime']) : '';
        if(empty($createtime)){
            return '';
        }
        return date('Y-m-d H:i',$createtime);
    }

    public function getThumbTextAttr($value, $data)
    {
        //获取到cdn的地址
        $cdnurl = config('site.cdnurl') ? config('site.cdnurl') : '';
        $cdnurl = trim($cdnurl, '/');
        
        // 多张图字符串结构
        $thumbs = isset($data['thumbs']) ? $data['thumbs'] : '';

        if(empty($thumbs))
        {
            $thumbs = "/assets/img/shop.jpg";
        }else
        {
            $thumbs = explode(',', $thumbs);
            if(!empty($thumbs))
            {
                $pic = '';

                foreach($thumbs as $item)
                {
                    if(is_file(".".$item))
                    {
                        $pic = $item;
                        break;
                    }
                }

                if(empty($pic))
                {
                    $pic = "/assets/img/shop.jpg";
                }

                $thumbs = $pic;
            }
        }

        return $cdnurl.$thumbs;
    }

    public function getThumbsTextAttr($value, $data)
    {
        //获取到cdn的地址
        $cdnurl = config('site.cdnurl') ? config('site.cdnurl') : '';
        $cdnurl = trim($cdnurl, '/');
        
        // 多张图字符串结构
        $thumbs = isset($data['thumbs']) ? $data['thumbs'] : '';
        $arr = explode(',', $thumbs);

        //返回结果
        $list = [];

        foreach($arr as $item)
        {
            if(is_file(".".$item))
            {
                $list[] = $cdnurl.$item;
            }
        }

        if(empty($list))
        {
            $list[] = $cdnurl."/assets/img/shop.jpg";
        }

        return $list;
    }

    // 省
    public function getProvinceTextAttr($value,$data){
        $region = empty($data['province']) ? '' : trim($data['province']);

        // 查询中文字
        return model('Region')->where(['code' => $region])->value('name');
    }

    // 市
    public function getCityTextAttr($value,$data){
        $region = empty($data['city']) ? '' : trim($data['city']);

        // 查询中文字
        return model('Region')->where(['code' => $region])->value('name');
    }

    // 区
    public function getDistrictTextAttr($value,$data){
        $region = empty($data['district']) ? '' : trim($data['district']);

        // 查询中文字
        return model('Region')->where(['code' => $region])->value('name');
    }

    public function getStatusTextAttr($value,$data)
    {
        $statusList = [0 => '未审核',1 => '已审核，未收货',2 => '已审核，未入库',3 => '已入库',-1 => '审核不通过'];
        $status = isset($data['status']) ? trim($data['status']) : '';
        if($status >= '-1'){
            return $statusList[$status];
        }
    }


    public function getAdminTextAttr($value,$data)
    {
        $adminid = $data['adminid'];

        if(empty($adminid))
        {
            return '';
        }

        return model('Admin')->where(['id' => $adminid])->value('nickname');
    }

    public function getReviewerTextAttr($value,$data)
    {
        $reviewerid = $data['reviewerid'];

        if(empty($reviewerid))
        {
            return '';
        }

        return model('Admin')->where(['id' => $reviewerid])->value('nickname');
    }

    public function getStromanTextAttr($value,$data)
    {
        $stromanid = $data['stromanid'];

        if(empty($stromanid))
        {
            return '';
        }

        return model('Admin')->where(['id' => $stromanid])->value('nickname');
    }

    public function business(){
        return $this->belongsTo('app\common\model\Business\User','busid','id',[],'LEFT')->setEagerlyType(0);
    }

    public function express(){
        return $this->belongsTo('app\common\model\Express','expressid','id',[],'LEFT')->setEagerlyType(0);
    }

    public function storage(){
        return $this->belongsTo('app\common\model\Depot\Storage','storageid','id',[],'LEFT')->setEagerlyType(0);
    }

    public function admin(){
        return $this->belongsTo('app\admin\model\Admin','adminid','id',[],'LEFT')->setEagerlyType(0);
    }

    public function reviewer(){
        return $this->belongsTo('app\admin\model\Admin','reviewerid','id',[],'LEFT')->setEagerlyType(0);
    }

    public function stroman(){
        return $this->belongsTo('app\admin\model\Admin','stromanid','id',[],'LEFT')->setEagerlyType(0);
    }
}
